package com.java.exer1;

/**
 * 三角形类
 * P=（a+b+c）/2 则：面积S=√p(p-a)(p-b)(p-c)
 *
 * @author shkstart
 * @create 2021-01-12 19:10
 */
public class Triangle extends Shape {
    public double side1;
    public double side2;
    public double side3;

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public Triangle(int x, int y, int x1, int y1, int x2, int y2) {
        this.side1 = Math.pow(((x1 > x) ? (x1 - x) * (x1 - x) : (x - x1) * (x - x1)) + ((y1 > y) ? (y1 - y) * (y1 - y) : (y - y1) * (y - y1)), 1.0 / 2);
        this.side2 = Math.pow((((x2 > x) ? (x2 - x) * (x2 - x) : (x - x2) * (x - x2)) + ((y2 > y) ? ((y2 - y) * (y2 - y)) : ((y - y2) * (y - y2)))), 1.0 / 2);
        this.side3 = Math.pow((((x2 > x1) ? (x2 - x1) * (x2 - x1) : (x1 - x2) * (x1 - x2)) + ((y1 > y2) ? ((y1 - y2) * (y1 - y2)) : ((y2 - y1) * (y2 - y1)))), 1.0 / 2);
    }

    public double getSide1() {
        return side1;
    }

    public void setSide1(double side1) {
        this.side1 = side1;
    }

    public double getSide2() {
        return side2;
    }

    public void setSide2(double side2) {
        this.side2 = side2;
    }

    public double getSide3() {
        return side3;
    }

    public void setSide3(double side3) {
        this.side3 = side3;
    }

    @Override
    public double area() {
        double p = (side1 + side2 + side3) / 2;
        double a = Double.parseDouble(String.format("%.2f", Math.pow((p * (p - side1) * (p - side2) * (p - side3)), 1.0 / 2)));
        return a;
    }

    @Override
    public double perimeter() {
        return Double.parseDouble(String.format("%.2f", side1 + side2 + side3));
    }

    @Override
    public String toString() {
        return "三角形:" + "\t" + "周长" + perimeter() + "\t" + "面积" + area();
    }
}
